<?php
namespace backend\controllers;

use Yii;
use yii\web\AdminController;
use backend\models\Subscription;
use PHPExcel;
use Symfony\Component\Console\Command\Command;
use phpDocumentor\Reflection\DocBlock\Tags\VarTest;
use common\helps\tools;

class EmailSubscribeController extends AdminController
{
    public $layout = false;
    //业务关键字
    public $service = 'EmailSubscribe';
    
    public function actionIndex()
    {
        return $this->render('index');
    }
    
    public function actionGetEmailSubscribe(){
        $data = Yii::$app->request->post();
        $subscribe_time = $data['subscription_time'];
        $subscription = Subscription::find()->select('email,username,city,create_time');
        if ($subscribe_time){
            $date_arr = explode(' - ', $subscribe_time);
            if (count($date_arr) == 2){
                $date_arr[0] = $date_arr[0] .= ' 00:00:00';
                $date_arr[1] = date('Y-m-d 00:00:00',strtotime($date_arr[1]. "+ 1 day"));
                $subscription = $subscription->where('create_time BETWEEN "'.$date_arr[0].'" AND "'.$date_arr[1].'"');
            }
        }
        if (tools::arrayHasValue($data,'is_export')){
            $list = $subscription->asArray()->all();
            $filed = ['username'=>'用户名','email'=>'邮箱地址','city'=>'城市信息','create_time'=>'提交时间'];
            $filename = 'email_subscribe_'.date('YmdHis').'.xls';
            $filename = iconv("utf-8", "gb2312", $filename);
            tools::exportExcel($filed, $list, $filename);
        }else{
            $sort_string = empty($data['columns'][$data['order'][0]['column']]['data']) ? 'id' : $data['columns'][$data['order'][0]['column']]['data'];
            $count = $subscription->count();
            $list = $subscription->offset($data['start'])->limit($data['length'])->orderBy(
                [$sort_string=>$data['order'][0]['dir'] == 'asc' ? SORT_ASC : SORT_DESC])->asArray()->all();
            $result['draw'] = intval($data['draw']);
            $result['recordsTotal'] = intval($count);
            $result['recordsFiltered'] = intval($count);
            $result['data'] = $list;
            return \yii\helpers\Json::encode($result);
        }
        //$key = Yii::$app->params['sys_redis_prefix'].'email_subscribe_'.session_id();
        //Yii::$app->redis->setex($key,Yii::$app->params['frontend_session_timeout'],json_encode($list,JSON_UNESCAPED_UNICODE));
        
    }
    
//     public function actionExportEmailSubscribe(){
//         $key = Yii::$app->params['sys_redis_prefix'].'email_subscribe_'.session_id();
//         $email_subscribe = Yii::$app->redis->get($key);
//         $email_subscribe = json_decode($email_subscribe,1);
//         if (count($email_subscribe) == 0){
//             $this->retJson('未查到可导出数据，请刷新页面重试',500);
//         }
//         $filed = ['username'=>'用户名','email'=>'邮箱地址','city'=>'城市信息','create_time'=>'提交时间'];
//         $filename = 'email_subscribe_'.date('YmdHis').'.xls';
//         $filename = iconv("utf-8", "gb2312", $filename);
//         tools::exportExcel($filed, $email_subscribe, $filename);
//     }
}